﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Dynamic;
using System.Collections;

namespace Facebook
{
    /// <summary>
    /// The <typeparamref name="Social.Facebook.Service.FacebookService"/> returns in all methods an instance of this object
    /// </summary>
    public class FacebookEntity : DynamicObject
    {
         private IDictionary<string, object> dictionary ;
        /// <summary>
        /// List of all response objects properties 
        /// </summary>
         public ArrayList Properties 
         { 
             get 
             {
                 if (dictionary.Keys.Contains("data"))
                     return dictionary["data"] as ArrayList;
                
                ArrayList al = new ArrayList();
                al.Add(dictionary);
                return al;
                
                
             } 
         }

        
        /// <summary>
        /// Url containing the facebook object graph to get the next results
        /// </summary>
         public String NextPage 
         {
             get 
             {
                 if (dictionary.Keys.Contains("paging")) 
                 {
                     if((dictionary["paging"] as Dictionary<string, object>).Keys.Contains("next"))
                        return (dictionary["paging"] as Dictionary<string, object>)["next"].ToString();
                     
                 }
                    

                 return null;
             }
         
         }


        /// <summary>
        /// Url containing the facebook object graph to get the previous results
        /// </summary>
         public String PreviousPage
         {
             get
             {
                 if (dictionary.Keys.Contains("paging")) 
                 {
                     if ((dictionary["paging"] as Dictionary<string, object>).Keys.Contains("previous"))
                        return (dictionary["paging"] as Dictionary<string, object>)["previous"].ToString();
                 }
                 
                 return null;
             }

         }

         /// <summary>
         /// Public constructor
         /// </summary>
         /// <param name="dictionary"></param>
         public FacebookEntity(IDictionary<string, object> dictionary) 
         {
             this.dictionary = dictionary;
         }
        
        /// <summary>
         /// For each property of this value try to get if it is a complex object, if so, recursively gets that dynamic object 
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
         public override bool TryGetMember(GetMemberBinder binder, out object result)
         {
             result = this.dictionary[binder.Name];

             if (result is IDictionary<string, object>)
             {
                 result = new FacebookEntity(result as IDictionary<string, object>);
             }
             else if (result is ArrayList && (result as ArrayList) is IDictionary<string, object>)
             {
                 result = new List<FacebookEntity>((result as ArrayList).ToArray().Select(x => new FacebookEntity(x as IDictionary<string, object>)));
             }
             else if (result is ArrayList)
             {
                 result = new List<object>((result as ArrayList).ToArray());
             }

             return this.dictionary.ContainsKey(binder.Name);
         }
    }
}